package cn.dnf.order.controller;

import cn.dnf.basic.util.JsonResult;
import cn.dnf.basic.util.LoginContext;
import cn.dnf.basic.util.PageList;

import cn.dnf.order.domain.OrderPetAcquisition;
import cn.dnf.order.dto.OrderDto;
import cn.dnf.order.query.OrderPetAcquisitionQuery;
import cn.dnf.order.service.IOrderPetAcquisitionService;

import cn.dnf.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

// @Controller
// @ResponseBody
@RestController // controller层，返回json格式数据
@RequestMapping("/orderPetAcquisition")
@Api(value = "订单的API",description="订单相关的CRUD功能")
public class OrderPetAcquisitionController {

    @Autowired
    private IOrderPetAcquisitionService orderPetAcquisitionService;



    /**
     *  分页数据
     * @return
     */
    @ApiOperation(value = "分页查询接口" )
    @PostMapping
    public PageList<OrderPetAcquisition> queryPage(@RequestBody OrderPetAcquisitionQuery query,HttpServletRequest request){
        try {

            Logininfo logininfo = LoginContext.getLogininfo(request);
            System.out.println(orderPetAcquisitionService.pageQuery(query,logininfo.getId()));

            return orderPetAcquisitionService.pageQuery(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<OrderPetAcquisition>();
        }
    }



    @ApiOperation(value = "确认订单接口" )
    @PostMapping("/confirm")
    public JsonResult confirm(@RequestBody OrderDto dto){
        try {
            orderPetAcquisitionService.confirm(dto);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("订单确认失败!");
        }
    }

    @ApiOperation(value = "取消订单接口" )
    @DeleteMapping("/{id}")
    public JsonResult cancel(@PathVariable("id") Long id){

        try {
            orderPetAcquisitionService.cancel(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("订单取消失败");
        }
    }
}
